package com.myapp.ruyixing.ui.widget;

import java.util.Calendar;

import android.app.Activity;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.app.TimePickerDialog.OnTimeSetListener;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;

public class TimePickerFragment extends DialogFragment {

	private OnTimeSetListener mOnTimeSetListener;

	public static TimePickerFragment getTimePickerFragment() {
		return new TimePickerFragment();
	}

	public static TimePickerFragment getTimePickerFragment(Calendar c) {
		TimePickerFragment fragment = getTimePickerFragment();

		Bundle bundle = new Bundle();
		bundle.putInt("hourofday", c.get(Calendar.HOUR_OF_DAY));
		bundle.putInt("minute", c.get(Calendar.MINUTE));

		fragment.setArguments(bundle);

		return fragment;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);

		if (activity instanceof OnTimeSetListener) {
			mOnTimeSetListener = (OnTimeSetListener) activity;
		}
	}

	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState) {
		int hourofday, minute;
		if (getArguments() != null) {
			hourofday = getArguments().getInt("hourofday");
			minute = getArguments().getInt("minute");
		} else {
			// Use the current date as the default date in the picker
			final Calendar c = Calendar.getInstance();
			hourofday = c.get(Calendar.HOUR_OF_DAY);
			minute = c.get(Calendar.MINUTE);
		}

		// Create a new instance of DatePickerDialog and return it
		return new TimePickerDialog(getActivity(), mOnTimeSetListener,
				hourofday, minute, true);
	}
}
